package com.wxtt.android.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fast.dev.frame.Adapter.recycleview.AbstractAdapter;
import com.fast.dev.frame.Adapter.recycleview.IItem;
import com.fast.dev.frame.Adapter.recycleview.stickyheader.StickyRecyclerHeadersAdapter;
import com.wxtt.android.R;
import com.wxtt.android.item.SampleItem;

import java.security.SecureRandom;
import java.util.List;

/**
 * 说明：StickyHeaderAdapter
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/24 0:54
 * <p/>
 * 版本：verson 1.0
 */
public class StickyHeaderAdapter extends AbstractAdapter implements StickyRecyclerHeadersAdapter{

    @Override
    public int getOrder() {
        return -100;
    }

    @Override
    public int getAdapterItemCount() {
        return 0;
    }

    @Override
    public List getAdapterItems() {
        return null;
    }

    @Override
    public IItem getAdapterItem(int position) {
        return null;
    }

    @Override
    public int getAdapterPosition(IItem item) {
        return -1;
    }

    @Override
    public int getGlobalPosition(int position) {
        return -1;
    }

    @Override
    public long getHeaderId(int position) {
        IItem item = getItem(position);
        if (item instanceof SampleItem && ((SampleItem)item).header != null){
            return ((SampleItem)item).header.charAt(0);
        }
        return -1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView;

        IItem item = getItem(position);
        if (item instanceof SampleItem && ((SampleItem) item).header != null) {
            //based on the position we set the headers text
            textView.setText(String.valueOf(((SampleItem) item).header.charAt(0)));
        }
        holder.itemView.setBackgroundColor(getRandomColor());
    }

    private int getRandomColor() {
        SecureRandom rgen = new SecureRandom();
        return Color.HSVToColor(150, new float[]{
                rgen.nextInt(359), 1, 1
        });
    }

}
